<?php
/**
 * User: b179a9
 * Date: 05.09.11
 * Time: 17:17
 */

namespace Managers;

class Orders extends BaseManager
{
    const TABLE = '\Catalog\Orders';
    protected $__table = self::TABLE;
    protected $__mapsTable = '\Catalog\Maps';
    protected $__usersTable = '\Catalog\Users';


    public function add($map_id, $user_id) {
        
        $map = $this->_em->find($this->__mapsTable, $map_id);
        
        if($map) {            
            $user = $this->_em->find($this->__usersTable, $user_id);
            
            if($user->getMoney() < $map->getPrice()) {
                throw new \Exception('You do not have enough money.');
            }
            
            $order = new \Catalog\Orders();
            $order->setDate(new \DateTime);            
            $order->setMap($map);  
            $order->setUser($user);
            $user->setMoney($user->getMoney() - $map->getPrice());
            $this->_em->persist($order);
            $this->_em->persist($user);
            $this->_em->flush();
            return $order;
            
        } else {
            throw new \Exception('Map does not exists');
        }

    }

//    public function add($user_id, $map_id, $transaction, $street, $city, $state, $country, $zip)
//    {
//        $order = new \Catalog\Orders();
//        $order->setDate(new \DateTime());
//        $order->setTrans($transaction);
//        $order->setMap($this->_em->getReference('\Catalog\Maps', $map_id));
//        $order->setUser($this->_em->getReference('\Catalog\Users', $user_id));
//        $order->setStreet($street);
//        $order->setCity($city);
//        $order->setState($state);
//        $order->setCountry($country);
//        $order->setZip($zip);
//
//        $this->_em->persist($order);
//        $this->_em->flush();
//        return $order;
//
//    }

    public function hasAccess($user_id, $map_id)
    {
        $obj = $this->_em->getRepository($this->__table)->findOneBy(array('user' => $user_id, 'map' => $map_id));

        
        return (boolean)$obj;
    }

    /**
     * Returns all orders by user.
     * @param $user_id
     * @return array
     */
    function getMaps($user_id)
    {
        $now = date('Y-m-d');
        $res = $this->_em->createQueryBuilder()
                ->select('o, m')
                ->from($this->__table, 'o')
                ->join('o.map', 'm')
                ->where('o.user = :id')->setParameter('id', $user_id)
                ->orderBy('o.id', 'desc')
                ->getQuery()
                ->getArrayResult();

        return $res;
    }


    function getCount($id)
    {

        $res = $this->_em->createQueryBuilder()
                ->select('o, m,count(o.map)')
                ->from($this->__table, 'o')
                ->join("o.map", "m")

                ->groupBy("o.map")
                ->getQuery()
                ->getArrayResult();

        return $res;
    }

    function getOwners($id)
    {

        $res = $this->_em->createQueryBuilder()
                ->select('o,u,m')
                ->from($this->__table, 'o')
                ->join("o.map", "m")
                ->join("o.user", "u")
                ->where("o.map= :id")
                ->setParameter("id", $id)
                ->getQuery()
                ->getArrayResult();

        return $res;


    }

    function getAvailableList($user_id) {
        $now = date('Y-m-d');
        $res = $this->_em->createQueryBuilder()
                ->select('o', 'm')
                ->from($this->__table, 'o')
                ->join('o.map', 'm')
                ->where('o.user = :user')->setParameter('user', $user_id)
                ->andWhere('m.dateend > :now')
                ->andWhere('m.datestart < :now')->setParameter('now', $now)
                ->getQuery()
                ->getArrayResult();
        
        return $res;
    }
}
